#ifndef XLCOMM_BASE_COUNTDOWNLATCH_H_
#define XLCOMM_BASE_COUNTDOWNLATCH_H_

#include <condition_variable>
#include <mutex>

#include "xlcomm/define.h"
#include "xlcomm/noncopyable.h"

namespace xlcomm {

// 线程安全的倒计数器
class XLCOMM_API CountDownLatch : noncopyable {
 public:
  explicit CountDownLatch(int count);

  // 阻塞等待计数归零，如果已归零则立即返回
  void WaitZero();

  // 计数减1。当计数减为0时，通知所有等待线程
  void CountDown();

  // 获取当前计数值
  int count() const;

 private:
  mutable std::mutex mutex_;
  std::condition_variable condition_;
  int count_;
};

}  // namespace xlcomm

#endif  // XLCOMM_BASE_COUNTDOWNLATCH_H_